var SHAInit = ($, algorithm, defaultTxt) => {
  const _algorithm = algorithm.toLowerCase();

  /*
  Store the calculated ciphertext here, so we can decrypt the message later.
  */
  let ciphertext;

  /*
  Fetch the contents of the "message" textbox, and encode it
  in a form we can use for the encrypt operation.
  */
  function getMessageEncoding() {
    const messageBox = $(".crypt-message");
    let message = messageBox.value;
    if(!message) {
      messageBox.value = defaultTxt || '360企业安全浏览器';
      message = messageBox.value;
    }
    return encode(message);
  }

  async function encryptFn(encoded){
    return await window.crypto.subtle.digest(
      {
          name: algorithm,
      },
      new Uint8Array(encoded)
    );
  }
  /*
  Get the encoded message, encrypt it and display a representation
  of the ciphertext in the "Ciphertext" element.
  */
  async function encryptMessage(msg) {
    let encoded = getMessageEncoding();
    ciphertext = await encryptFn(encoded);

    $('.ciphertext').style.display = 'block';

    let buffer = Array.from(new Uint8Array(ciphertext)); // convert buffer to byte array
    buffer = buffer.map(b => b.toString(16).padStart(2, '0')).join('').toUpperCase(); // convert bytes to hex string
    
    const ciphertextValue = $(".ciphertext-value");
    ciphertextValue.classList.add('fade-in');
    ciphertextValue.addEventListener('animationend', () => {
      ciphertextValue.classList.remove('fade-in');
    });
    ciphertextValue.value = `${buffer}`;
  }

  const initHtml = () => {
    var node = document.querySelector(".sha-html").cloneNode(true);
    node.classList.remove('sha-html');
    node.classList.add(_algorithm);
    document.querySelector('.sha-html').parentElement.appendChild(node);


    addTestNode(algorithm, _algorithm);
  }
  initHtml();


  const encryptButton =$(".encrypt-button");
  encryptButton.addEventListener("click", () => {
    encryptMessage();
  });

  window.allPerformance = window.allPerformance || [];
  allPerformance.push({
    name: algorithm, fn: getTestFn(encryptFn), resultId: `#tr-${_algorithm}`
  })
};
